<?php

/**
 * @author      Alex Bilbie <hello@alexbilbie.com>
 * @copyright   Copyright (c) Alex Bilbie
 * @license     http://mit-license.org/
 *
 * @link        https://github.com/thephpleague/oauth2-server
 */

declare(strict_types=1);

namespace app\oauth\Repositories;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\UserEntityInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use app\oauth\Entities\UserEntity;

use app\oauth\model\User as UserModel; //用户模型

class UserRepository implements UserRepositoryInterface
{
    /**
     * @return UserEntityInterface
     */
    public function getUserEntityByUserCredentials(
        $username,
        $password,
        $grantType,
        ClientEntityInterface $clientEntity
    ) {
        // 验证用户时调用此方法
        // 用于验证用户信息是否符合
        // 可以验证是否为用户可使用的授权类型($grantType)与客户端($clientEntity)
        // 验证成功返回 UserEntityInterface 对象
        $user = UserModel::where('username', $username)->where('password', $password)->find();
        
        if (empty($user)) {
            return;
        }
        $user_id = $user->id;

    

        $user_entity = new UserEntity();
        $user_entity->setIdentifier($user_id);
        
        return $user_entity;
    }
}
